import { useState } from "react"
import { uploadFile } from "../../api/http";
import './index.css';

export const FileUpload = (props) => {

    const [file, setFile] = useState(null);
    const fileChangeHandler = (e) => {
        setFile(e.target.files[0])
    }
    
    const uploadClickHandler = () => {
        if (file === null) {
            return;
        }
        const formData = new FormData();
        formData.append('file', file);
        uploadFile('/upload', formData).then(res => {
            console.log(res);
            props.reload();
        })
    }

    return (
        <form>
            <input type="file" name="file" onChange={fileChangeHandler}/>
            <button type="button" onClick={uploadClickHandler}>上传</button>
        </form>
    )
}

